package org.jsola.hr.service.impl;

import org.jsola.core.Page;
import org.jsola.core.entity.BaseCreateDO;
import org.jsola.core.entity.BaseDO;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.common.HrLogKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IApplyToProgLevelDAO;
import org.jsola.hr.dto.ApplyToProgLevelAddDTO;
import org.jsola.hr.dto.ApplyToProgLevelUpdateDTO;
import org.jsola.hr.entity.ApplyToProgLevelDO;
import org.jsola.hr.query.ApplyToProgLevelQuery;
import org.jsola.hr.service.IApplyToProgLevelService;
import org.jsola.hr.vo.ApplyToProgLevelListVO;
import org.jsola.hr.vo.ApplyToProgLevelVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhr
 */
@Service("hrApplyToProgLevelServiceImpl")
public class ApplyToProgLevelServiceImpl implements IApplyToProgLevelService {

    @Autowired
    private IApplyToProgLevelDAO applyToProgLevelDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public ApplyToProgLevelVO save(ApplyToProgLevelAddDTO applyToProgLevelAddDTO, TokenUser tokenUser) {
        // 转DO
        ApplyToProgLevelDO applyToProgLevelDO = applyToProgLevelAddDTO.to(ApplyToProgLevelDO.class);
        // 保存
        applyToProgLevelDO = save(applyToProgLevelDO, tokenUser.getSiteId(), tokenUser.getUserId());
        HrLogKit.logForSave("新增方案适用设置职级", "saveApplyToProgLevel", applyToProgLevelDO.getId(), tokenUser);
        return applyToProgLevelDO.to(ApplyToProgLevelVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(ApplyToProgLevelUpdateDTO applyToProgLevelUpdateDTO, TokenUser tokenUser) {
        //转DO
        ApplyToProgLevelDO applyToProgLevelDO = applyToProgLevelUpdateDTO.to(ApplyToProgLevelDO.class);
        //根据主键更新，只更新非null值
        HrLogKit.logForUpdate("修改方案适用设置职级", "updateApplyToProgLevel", applyToProgLevelDO.getId(), tokenUser);
        return updateByIdSelective(applyToProgLevelDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... applyToProgLevelIds) {
        HrLogKit.logForDelete("批量删除方案适用设置职级", "deleteApplyToProgLevelByIds"
                , Arrays.toString(applyToProgLevelIds), tokenUser);
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) applyToProgLevelIds);
    }


    @Override
    public ApplyToProgLevelVO selectById(String applyToProgLevelId, String siteId) {
        ApplyToProgLevelDO applyToProgLevelDO = selectDOById(applyToProgLevelId, siteId);
        if (applyToProgLevelDO == null) {
            return null;
        }
        return applyToProgLevelDO.to(ApplyToProgLevelVO.class);
    }

    @Override
    public List<ApplyToProgLevelListVO> select(ApplyToProgLevelQuery applyToProgLevelQuery, String siteId) {
        List<ApplyToProgLevelDO> applyToProgLevelDOList = selectDO(applyToProgLevelQuery, siteId);
        if (CollectionUtils.isEmpty(applyToProgLevelDOList)) {
            return applyToProgLevelDOList == null ? null : new ArrayList<>();
        }
        return applyToProgLevelDOList.stream()
                .map(applyToProgLevelDO -> applyToProgLevelDO.to(ApplyToProgLevelListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(ApplyToProgLevelQuery applyToProgLevelQuery, String siteId) {
        Example example = buildExample(applyToProgLevelQuery, siteId);
        return applyToProgLevelDAO.selectCountByExample(example);
    }

    @Override
    public Page<ApplyToProgLevelListVO> selectPage(ApplyToProgLevelQuery applyToProgLevelQuery, String siteId) {
        Example example = buildExample(applyToProgLevelQuery, siteId);
        Page<ApplyToProgLevelDO> page = applyToProgLevelDAO.selectPageByExample(example,
                applyToProgLevelQuery.getPageNo(),
                applyToProgLevelQuery.getPageSize());

        return page.to(ApplyToProgLevelListVO.class);
    }

    @Override
    public ApplyToProgLevelDO selectDOById(String applyToProgLevelId, String siteId) {
        return listById(applyToProgLevelId, siteId);
    }

    @Override
    public List<ApplyToProgLevelDO> selectDO(ApplyToProgLevelQuery applyToProgLevelQuery, String siteId) {
        Example example = buildExample(applyToProgLevelQuery, siteId);
        return applyToProgLevelDAO.selectByExample(example);
    }


    @Override
    public Integer insertList(List<ApplyToProgLevelAddDTO> applyToProgLevelAddDTOList, String progId, TokenUser tokenUser) {
        List<ApplyToProgLevelDO> applyToProgLevelDOList = applyToProgLevelAddDTOList.stream().map(applyToProgLevelAddDTO -> {
            ApplyToProgLevelDO applyToProgLevelDO = applyToProgLevelAddDTO.to(ApplyToProgLevelDO.class);
            applyToProgLevelDO.setSiteId(tokenUser.getSiteId());
            applyToProgLevelDO.setCreateUserId(tokenUser.getUserId());
            applyToProgLevelDO.setGmtCreate(new Date());
            applyToProgLevelDO.setProgId(progId);
            return applyToProgLevelDO;
        }).collect(Collectors.toList());
        HrLogKit.logForSave("批量新增方案适用设置职级", "insertListToProgLevel", applyToProgLevelDOList
                .parallelStream().map(BaseDO::getId).collect(Collectors.joining(",", "[", "]")), tokenUser);
        return applyToProgLevelDAO.insertListAndSetId(applyToProgLevelDOList);
    }

    @Override
    public Integer deleteByProgId(String progId, TokenUser tokenUser) {
        ApplyToProgLevelDO applyToProgLevelDO = new ApplyToProgLevelDO();
        applyToProgLevelDO.setProgId(progId);
        applyToProgLevelDO.setSiteId(tokenUser.getSiteId());
        HrLogKit.logForDelete("根据方案id删除方案适用设置职级（真实删除）", "deleteByProgId", progId, tokenUser);
        return applyToProgLevelDAO.delete(applyToProgLevelDO);
    }

    /**
     * 根据查询参数，构建example
     *
     * @param applyToProgLevelQuery 查询参数
     * @param siteId                所属站点id
     * @return example
     */
    private Example buildExample(ApplyToProgLevelQuery applyToProgLevelQuery, String siteId) {
        Example example = new Example(ApplyToProgLevelDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        if (applyToProgLevelQuery.getProgId() != null) {
            example.and().andLike("progId", "%" + applyToProgLevelQuery.getProgId() + "%");
        }
        if (applyToProgLevelQuery.getLevelId() != null) {
            example.and().andLike("levelId", "%" + applyToProgLevelQuery.getLevelId() + "%");
        }
        if (applyToProgLevelQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", applyToProgLevelQuery.getCompanyId());
        }
        if (applyToProgLevelQuery.getGmtCreateStart() != null &&
                applyToProgLevelQuery.getGmtCreateEnd() != null) {
            example.and().andBetween("gmtCreate", applyToProgLevelQuery.getGmtCreateStart(),
                    applyToProgLevelQuery.getGmtCreateEnd());
        }
        if (applyToProgLevelQuery.getCreateUserId() != null) {
            example.and().andLike("createUserId", "%" + applyToProgLevelQuery.getCreateUserId() + "%");
        }
        // 排序
        ExampleKit.setExampleOrder(example, applyToProgLevelQuery.getOrders());
        return example;
    }
}




